package com.wpp.websocket.config;

import com.wpp.websocket.handler.WsHandler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * @author thisisit
 * @class: WebSocketConfiguration
 * @desc: TODO
 * @date: 2021/6/17 21:41
 */
@EnableWebSocket
@Configuration
public class WebSocketConfiguration implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(myHandler(), "/handler")
                .setAllowedOriginPatterns("*")
                .setAllowedOrigins("*")
                .addInterceptors(new HttpSessionHandshakeInterceptor())
                .addInterceptors(wsInterceptor());
    }

    @Bean
    public WebSocketHandler myHandler() {
        return new WsHandler();
    }

    @Bean
    public WsInterceptor wsInterceptor() {
        return new WsInterceptor();
    }
}
